//
//  personalDataViewController.m
//  YiXiu
//
//  Created by ZHY on 16/6/30.
//  Copyright © 2016年 zzz. All rights reserved.
//

#define filePath [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"userInfo.archive"]


#import "personalDataViewController.h"
#import "perNikeNameController.h"

#import "chooseAgeVC.h"
#import "chooseSexView.h"
#import "sexChooseVC.h"
#import "chooseMarriedVC.h"

#import "MYSSerializeKit.h"

@interface personalDataViewController ()

//设置昵称右边的昵称label
@property (nonatomic, strong) NSString * nikeNaLab;
@property (nonatomic, strong) chooseSexView *chooesSex;
@property (nonatomic, weak) __block NSString * ageStr;
@property (nonatomic,weak)  __block NSString * marStr;



@end

@implementation personalDataViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"个人资料";
    
    MYSSERIALIZE_UNARCHIVE(self.userModel, @"userModel", filePath);
    
    NSLog(@"%@",self.userModel.married);
    
    
    self.userModel = [UserModel sharedUserModel];
    
    //加载数据
    [self addDataWithRefreshData:NO];
    
}

-(void)addRightBtn{
    

    UIButton * rightItem = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 64, 44)];
//    [rightItem addTarget:self action:@selector(releaseInfo:) forControlEvents:UIControlEventTouchUpInside];
    [rightItem setTitle:@"编辑" forState:UIControlStateNormal];
    [rightItem setTitleColor:[UIColor grayColor] forState:UIControlStateSelected];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightItem];
    
    
    
    
    
}


//增加数据
- (void)addDataWithRefreshData:(BOOL)isOK{
    [self addOneGroupWithRefreshData:isOK];
    [self addTwoGroupWithRefreshData:isOK];
    [self addThreeGroupWithRefreshData:isOK];
    [self addFourGroupWithRefreshData:isOK];
    if (isOK == YES) {
        [self.tableView reloadData];
    }
}

//增加view的动画效果，整改两个运行编译bug，更改一些方法，使得运行更加流畅
//第一组
- (void)addOneGroupWithRefreshData:(BOOL)isOK{

    EventSettingItem *setting = [EventSettingItem itemWithText:@"头像" image:nil type:EventSettingItemTypeArrow viewTag:9 arrowImageName:nil];
    setting.operation = ^{
       
        
    };

    EventSettingItem *setting1 = [EventSettingItem itemWithImage:[UIImage imageNamed:@"icon_12_2"] title:@"昵称" type:EventSettingItemTypeArrow desc:self.userModel.name descColor:[UIColor redColor] viewTag:buildScheduleDateType];
    setting1.operation = ^{
        
        __weak typeof (self) weakSelf = self;
        perNikeNameController * per = [[perNikeNameController alloc]init];
        per.Block = ^(NSString *nickName){
            weakSelf.userModel.name = nickName;
        };
        [weakSelf.navigationController pushViewController:per animated:YES];
    };

    EventSettingGroup *group = [[EventSettingGroup alloc] init];
    group.rowHeight = 60;
    
    group.items = @[setting,setting1];
    if (isOK) {
        [self.allGroups replaceObjectAtIndex:0 withObject:group];
    }else{
        [self.allGroups addObject:group];
    }
    
}

-(void)addTwoGroupWithRefreshData:(BOOL)isOK
{
    EventSettingItem *setting1 = [EventSettingItem itemWithText:@"用户须知" image:nil type:EventSettingItemTypeArrow viewTag:1 arrowImageName:@"icon_22"];
    
    setting1.operation = ^{
        
  
    };
    
    
    EventSettingGroup *group = [[EventSettingGroup alloc] init];
    
    group.rowHeight = 60;

    group.items = @[setting1];
    
    if (isOK) {
        [self.allGroups replaceObjectAtIndex:1 withObject:group];
    }else{
        [self.allGroups addObject:group];
    }
    
    
}

-(void)addThreeGroupWithRefreshData:(BOOL)isOK
{
     EventSettingItem *setting = [EventSettingItem itemWithImage:[UIImage imageNamed:@"icon_12_2"] title:@"性别" type:EventSettingItemTypeArrow desc:self.userModel.sex descColor:[UIColor redColor] viewTag:buildScheduleDateType];
    
    setting.operation = ^{
        [self showChooesSexView];
    };
    
    
    EventSettingItem *setting1 = [EventSettingItem itemWithImage:[UIImage imageNamed:@"icon_12_2"] title:@"年龄" type:EventSettingItemTypeArrow desc:self.userModel.age descColor:[UIColor redColor] viewTag:buildScheduleDateType];
    
    setting1.operation = ^{
        chooseAgeVC * chooseAge = [[chooseAgeVC alloc]init];
        
        __weak typeof (self) weakSelf = self;
        [chooseAge setBlock:^(NSString *ageLab) {
            
            NSLog(@"%@",ageLab);
            weakSelf.ageStr = [NSString stringWithFormat:@"%@",ageLab];
            // 刷新表示图
            [weakSelf.tableView reloadData];
            //加载数据
            [weakSelf addDataWithRefreshData:YES];
            
        }];
        
        [self.navigationController pushViewController:chooseAge animated:YES];

        
    };
    
    
   EventSettingItem *setting2 = [EventSettingItem itemWithImage:[UIImage imageNamed:@"icon_12_2"] title:@"性取向" type:EventSettingItemTypeArrow desc:self.userModel.sexSelect descColor:[UIColor redColor] viewTag:buildScheduleDateType];
    
    setting2.operation = ^{
        sexChooseVC * sexChoose = [[sexChooseVC alloc]init];
        [self.navigationController pushViewController:sexChoose animated:YES];
    };
    
    
      EventSettingItem *setting3 = [EventSettingItem itemWithImage:[UIImage imageNamed:@"icon_12_2"] title:@"婚姻状况" type:EventSettingItemTypeArrow desc: self.userModel.married descColor:[UIColor redColor] viewTag:buildScheduleDateType];
    
    setting3.operation = ^{
        chooseMarriedVC * chooseMar = [[chooseMarriedVC alloc]init];
         __weak typeof (self) weakSelf = self;
        [chooseMar setBlock:^(NSString *marText) {
            weakSelf.marStr = marText;
            weakSelf.userModel.married = marText;
            
            [self addDataWithRefreshData:YES];
        }];
        
        [self.navigationController pushViewController:chooseMar animated:YES];
    };
    
    EventSettingGroup *group = [[EventSettingGroup alloc] init];
    
    group.rowHeight = 60;
    
    group.items = @[setting,setting1,setting2,setting3];
    
    
    if (isOK) {
        [self.allGroups replaceObjectAtIndex:2 withObject:group];
    }else{
        [self.allGroups addObject:group];
    }

}

-(void)addFourGroupWithRefreshData:(BOOL)isOK{
    
    
     EventSettingItem *setting = [EventSettingItem itemWithImage:[UIImage imageNamed:@"icon_12_2"] title:@"退出登录" type:EventSettingItemTypeNone desc:self.userModel.sexSelect descColor:[UIColor redColor] viewTag:buildScheduleDateType];
    
    setting.operation = ^{
        
        [UIAlertController showAlertViewWithTitle:@"确认退出登录" message:nil cancle:@"取消" sureTitle:@"确定" sureAction:^(UIAlertAction *action) {
            
            [[UserModel sharedUserModel] setIsLogin:NO];
            [[UserModel sharedUserModel] setName:@"未登录"];
            
            UIImage *heardImg = [UIImage imageNamed:@"NoLoginHead"];
            
            [[UserModel sharedUserModel] setHeadImage:(NSString *)heardImg];
            
            [NSKeyedArchiver archiveRootObject:[UserModel sharedUserModel] toFile:UserPath];
            
            [self.navigationController popViewControllerAnimated:YES];
            
        }];
        
    };
    
    
    EventSettingGroup *group = [[EventSettingGroup alloc] init];
    
    group.rowHeight = 60;
    
    group.items = @[setting];
    
    
    if (isOK) {
        [self.allGroups replaceObjectAtIndex:3 withObject:group];
    }else{
        [self.allGroups addObject:group];
    }
    
}


- (chooseSexView *)chooesSex{
    if (!_chooesSex) {
         _chooesSex = [[chooseSexView alloc]init];
        [self.view addSubview:_chooesSex];
    }
    return _chooesSex;
}







- (void)showChooesSexView{
    
    [UIView animateWithDuration:1 animations:^{
        self.chooesSex.frame = CGRectMake(0, KScreenHeight/2, KScreenWidth, KScreenHeight/2);
    }];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
  
    
    MYSSERIALIZE_ARCHIVE(self.userModel, @"userModel", filePath);
//
    MYSSERIALIZE_UNARCHIVE(self.userModel, @"userModel", filePath);
    
    NSLog(@"%@",self.userModel.married);
    
}


@end
